﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace Thallo {

    public abstract class ParameterParser : IParameterParser
    {
        //public string Prefix { get;  set; }

        public Type ValueType { get;  set; }

        public ParameterParser() { }

        public ParameterParser(Type valueType) {
            this.ValueType = valueType;      
        }

        public abstract string ParseKey(object val);

        public IParameterParser Clone() {
            return this.MemberwiseClone() as IParameterParser;
        }
    }

    sealed class DefaultParameterParser : ParameterParser {
        const string NULL_KEY = "_";
        static Newtonsoft.Json.JsonSerializer _serializer = new Newtonsoft.Json.JsonSerializer();

        public DefaultParameterParser(Type valueType):base(valueType) { }

        public override string ParseKey(object val) {
            if (val == null)
                return string.Empty;
            else if (ValueType.IsValueType)
                return val.GetHashCode().ToString();

            var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            _serializer.Serialize(sw, val);
            var result = sb.ToString();
            return result.GetHashCode().ToString();
        }

    }
}
